//
// Created by xiongqimin on 2022/8/5.
//

#include <iostream>
#include "common_audio/vad/include/webrtc_vad.h"

int main(int argc, char *argv[]) {
    const size_t kMaxFrameLength = 1440;

    const int kModes[] = {0, 1, 2, 3};
    const size_t kModesSize = sizeof(kModes) / sizeof(*kModes);

    // 创建 Handle
    VadInst* handle = WebRtcVad_Create();
    int16_t zeros[kMaxFrameLength] = {0};

    // Construct a speech signal that will trigger the VAD in all modes. It is
    // known that (i * i) will wrap around, but that doesn't matter in this case.
    int16_t speech[kMaxFrameLength];
    for (size_t i = 0; i < kMaxFrameLength; i++) {
        speech[i] = static_cast<int16_t>(i * i);
        zeros[i] = (i % 80 == 0) ? 80 : ( (i % 159) == 0 ? -80 : 0);
    }
    std::shared_ptr<FILE> f;
    f.reset(fopen("D:/code/numax/tool/webrtc-audio-processing/webrtc/test/vad/vad.pcm", "rb"), [](FILE *f){
        fclose(f);
    });

    WebRtcVad_Init(handle);
    // 0~3, 值越大静音效果越好，反之则过滤得不干净
    WebRtcVad_set_mode(handle, 1);
    int8_t vad[320] = {0};
    while (fread(vad, 320, 1, f.get()) > 0) {
        std::cout << WebRtcVad_Process(handle, 8000, reinterpret_cast<const int16_t *>(vad), 160) << std::endl;
    }
    WebRtcVad_Free(handle);
}